/**
 * Copyright (c) 2015-2018, Golly Hu 胡高 (3722711@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.hg.jfsa.common.directive;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.jfinal.kit.StrKit;
import com.jfinal.template.Directive;
import com.jfinal.template.Env;
import com.jfinal.template.expr.ast.Assign;
import com.jfinal.template.expr.ast.Expr;
import com.jfinal.template.expr.ast.ExprList;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;

import cn.hg.base.kit.RequestKit;
import cn.hg.jfsa.core.JfsaThreadLocal;
import cn.hg.jfsa.web.theme.ThemeManager;

/**
 * @author Golly
 */
public abstract class BaseDirective extends Directive {

	@Override
	public void exec (Env env, Scope scope, Writer writer) {
		scope = new Scope(scope);
		scope.getCtrl().setLocalAssignment();
		this.exprList.eval(scope);
		this.onRender(env, scope, writer);
	}

	public Map<String, Object> getAssignedParaMap (Scope scope) {
		Map<String, Object> paraMap = new HashMap<>();

		for (int i = 0; i < this.exprList.length(); i++) {

			for (Expr e : this.exprList.getExprArray()) {

				if (e instanceof Assign) {
					Assign a = (Assign) e;

					if (StrKit.notBlank(a.getId())) {
						paraMap.put(a.getId(), a.getRight().eval(scope));
					}

				}

			}

		}

		paraMap.put("theme", ThemeManager.me().getCurrentTheme().getId());
		paraMap.put("device", RequestKit.isMoblieBrowser(JfsaThreadLocal.getRequest()) ? "h5" : "pc");
		return paraMap;
	}

	public <T> T getPara (int index, Scope scope) {
		return this.getPara(index, scope, null);
	}

	@SuppressWarnings ("unchecked")
	public <T> T getPara (int index, Scope scope, T defaultValue) {

		if (index < 0 || index >= this.exprList.length()) {
			return defaultValue;
		}

		Object data = this.exprList.getExpr(index).eval(scope);
		return (T) (data == null ? defaultValue : data);
	}

	public <T> T getPara (String key, Scope scope) {
		return this.getPara(key, scope, null);
	}

	@SuppressWarnings ("unchecked")
	public <T> T getPara (String key, Scope scope, T defaultValue) {
		Object data = scope.getLocal(key);
		return (T) (data == null ? defaultValue : data);
	}

	public abstract void onRender (Env env, Scope scope, Writer writer);

	public void renderBody (Env env, Scope scope, Writer writer) {
		this.stat.exec(env, scope, writer);
	}

	public void renderText (Writer writer, String text) {

		try {
			writer.write(text);
		}
		catch (IOException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void setExprList (ExprList exprList) {
		super.setExprList(exprList);
	}

}
